package com.ruoyi.contest.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.contest.domain.ContestInfo;
import com.ruoyi.contest.domain.SubContest;
import com.ruoyi.contest.domain.vo.SubContestQueryVo;
import com.ruoyi.contest.mapper.ContestInfoMapper;
import com.ruoyi.contest.mapper.SubContestMapper;
import com.ruoyi.contest.service.ISubContestService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 子竞赛信息Service业务层处理
 *
 * @author ruoyi
 * @date 2023-02-14
 */
@Service
@RequiredArgsConstructor
public class SubContestServiceImpl extends ServiceImpl<SubContestMapper, SubContest> implements ISubContestService
{

    private final ContestInfoMapper contestInfoMapper;


    /**
     * 查询子竞赛信息列表
     *
     * @param subContest 子竞赛信息
     * @return 子竞赛信息
     */
    @Override
    public List<SubContest> selectSubContestList(SubContest subContest)
    {
        return baseMapper.selectSubContestList(subContest);
    }

    @Override
    public List<SubContest> selectSubContestListWithParent(SubContestQueryVo subContest) {
        List<SubContest> list = baseMapper.selectSubContestList2(subContest);
        if (list!=null){
            for (SubContest subContest1 : list) {
                Map<String, Object> params=new HashMap<>();
                ContestInfo parentContest = contestInfoMapper.selectById(subContest1.getContestId());
                params.put("parentName", parentContest.getName());
                params.put("url", parentContest.getUrl());
                params.put("brief", parentContest.getBrief());
                params.put("freq", parentContest.getFreq());
                params.put("fee", subContest1.getFee());
                params.put("inMinistry",parentContest.getInMinistry());
                params.put("grade",parentContest.getGrade());
                subContest1.setParams(params);
            }
        }
        return list;
    }

    @Override
    public int changeSubEnable(SubContest subContest){
        return baseMapper.changeSubEnable(subContest);
    }

    @Override
    public boolean makeInsert() {
        Integer nowYear = getTimeYear();
        Integer beforeYear = nowYear - 1;
        try {
            LambdaQueryWrapper<SubContest> lqw = new LambdaQueryWrapper<SubContest>();
            lqw.eq(SubContest::getYear,beforeYear);

            //执行查询
            List<SubContest> subContestsList = baseMapper.selectList(lqw);

            //复制数据操作
            for(SubContest subContestX : subContestsList){
                if(subContestX.getYear() == Long.valueOf(nowYear)){
                    continue;
                } else {
                    SubContest subcontest = new SubContest();
                    subcontest.setContestId(subContestX.getContestId());
                    subcontest.setName(subContestX.getName());
                    subcontest.setUrl(subContestX.getUrl());
                    subcontest.setAttachmentsUrl(subContestX.getAttachmentsUrl());
                    subcontest.setYear(Long.valueOf(nowYear));
                    subcontest.setLevel(subContestX.getLevel());
                    subcontest.setCompetitionDate(subContestX.getCompetitionDate());
                    subcontest.setFee(subContestX.getFee());
                    subcontest.setRemark(subContestX.getRemark());
                    subcontest.setDelFlag(subContestX.getDelFlag());
                    subcontest.setCreateBy(subContestX.getCreateBy());
                    subcontest.setCreateTime(subContestX.getCreateTime());
                    subcontest.setUpdateBy(subContestX.getUpdateBy());
                    subcontest.setUpdateTime(subContestX.getUpdateTime());
                    baseMapper.insert(subcontest);
                }
            }
            System.out.println("生成成功");
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return true;
    }

    //获取当前时间
    public int getTimeYear(){
        Calendar time = Calendar.getInstance();
        int year = time.get(Calendar.YEAR);
        return year;
    }




}
